package com.hsq.ndmall.mall.service.impl;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

import com.hsq.ndmall.mall.dao.SystemDao;
import com.hsq.ndmall.mall.entity.SystemEntity;
import com.hsq.ndmall.mall.service.SystemService;


@Service("systemService")
public class SystemServiceImpl extends ServiceImpl<SystemDao, SystemEntity> implements SystemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SystemEntity> page = this.page(
                new Query<SystemEntity>().getPage(params),
                new QueryWrapper<SystemEntity>()
        );

        return new PageUtils(page);
    }
    public Map<String, String> queryAll() {
        List<SystemEntity> systemList = baseMapper.selectList(new QueryWrapper<SystemEntity>()
                .lambda()
                .eq(SystemEntity::getDeleted,false));
        Map<String, String> systemConfigs = new HashMap<>();
        for (SystemEntity item : systemList) {
            systemConfigs.put(item.getKeyName(), item.getKeyValue());
        }

        return systemConfigs;
    }

    public void addConfig(String key, String value) {
        SystemEntity system = new SystemEntity();
        system.setKeyName(key);
        system.setKeyValue(value);
        system.setAddTime(LocalDateTime.now());
        system.setUpdateTime(LocalDateTime.now());
        baseMapper.insert(system);
    }
}
